﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KabiManagementWeb.Models;
namespace KabiManagementWeb.Controllers
{
    public class EmployeeController : Controller
    {
        KBManagementDataContext context = DBUtils.GetContext(DBUtils.DBType.Local);
        
        #region Employee
        //
        // GET: /Employee/

        public ActionResult Index()
        {

            ViewData["TimeSlot"] = new SelectList(context.TimeSlots.ToList<TimeSlot>(), "ID", "Ts_Name");
            ViewData["Position"] = new SelectList(context.Positions.ToList<Position>(), "ID", "Ps_Name");
            return View(context.Employees);
        }

        //
        // GET: /Employee/Details/5

        public ActionResult Details(string id)
        {
            var employee = context.Employees.Single(x => x.ID.Equals(id));
            return View(employee);
        }

        //
        // GET: /Employee/Create

        public ActionResult Create()
        {

            ViewData["TimeSlot"] = new SelectList(context.TimeSlots.ToList<TimeSlot>(), "ID", "Ts_Name");
            ViewData["Position"] = new SelectList(context.Positions.ToList<Position>(), "ID", "Ps_Name");
            return View();
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(Employee employee)
        {
            try
            {
                // TODO: Add insert logic here
                employee.Status = true;
                context.Employees.InsertOnSubmit(employee);
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Employee/Edit/5

        public ActionResult Edit(string id)
        {
            ViewData["TimeSlot"] = new SelectList(context.TimeSlots.ToList<TimeSlot>(), "ID", "Ts_Name");
            ViewData["Position"] = new SelectList(context.Positions.ToList<Position>(), "ID", "Ps_Name");
            var employee = context.Employees.Single(x => x.ID.Equals(id));
            return View(employee);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, Employee collection)
        {
            try
            {
                var employee=context.Employees.Single(x=>x.ID.Equals(id));
                employee.ID = collection.ID;
                employee.FullName = collection.FullName;
                employee.Email = collection.Email;
                employee.CreditCard = collection.CreditCard;
                employee.BirthDay = collection.BirthDay;
                employee.Address = collection.Address;
                employee.Experience = collection.Experience;
                employee.Gender = collection.Gender;
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Employee/Delete/5

        public ActionResult Delete(string id)
        {
            return View();
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, Employee collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion
      

    }
}
